'use strict';

const Controller = require('egg').Controller;

class BuildController extends Controller {

 

  // 添加书本    商店
  async add() {
    const { ctx } = this;
 

    // const {
    //     uid,
    //     name,
    //     cover,
    //     wirter,
    //     type,
    //     dec,
    //     recommended,
    //     general,
    //     no,
    //     readnum,
    //     wrodnum,
    //     humnum} = ctx.request.body; // 得到post请求的body参数


    let {
      uid,
      name,
      img,
      num,
      money,
      time,
      address,
      askdec,
      specask,
      bumoney,
      lat,
      lng,
      state,

     } = ctx.request.body; 
       
        state = state || 0;
    // 数据过滤
    // if (!name) return ctx.body = { success: false, info: 'name不能为空' };
    // if (!wirter) return ctx.body = { success: false, info: 'wirter不能为空' };
    // if (!type) return ctx.body = { success: false, info: 'type不能为空' };
    // if (!cover) return ctx.body = { success: false, info: 'cover不能为空' };
    console.log(ctx.request.body,'我是客户端传递过来的值');
    console.log(lat,lng,'我是经纬度');
    try {

      
      await ctx.model.Foot.create({
        uid,
        name,
        img,
        num,
        money,
        time,
        address,
        askdec,
        specask,
        bumoney,
        lat,
        lng,
        state,
    });

 
      ctx.body = { success: true, info: '添加成功' };

    } catch (e) {
      console.log('错误xcxi', e);
      ctx.body = { success: false, info: '添加失败' };
    }

  }

  // 删

  async remove() {
    const { ctx } = this;
    const { uid } = ctx.request.body;

    if (!uid) return ctx.body = { success: false, info: 'uid不能为空' };

    try {
      await ctx.model.Foot.destroy({
        where: {
          uid,
        },
      });

      ctx.body = { success: true, info: '删除成功' };

    } catch (e) {
      ctx.body = { success: false, info: '删除失败' };
    }
  }



    // 查询多个
    async bookInfo() {
      const { ctx,app } = this;
      const { state } = ctx.request.body;
      const { Op } = app.Sequelize;
     
      
      try {
        const one = await ctx.model.Foot.findAll({ where: {
          state  //模糊查询
        },});

        console.log(one);
        
          ctx.body = { success: true, data: one, info: '查询成功' };
        
        
      } catch (e) {
        console.log(e);
        ctx.body = { success: false, info: '查询失败' };
      }
    }


    //查询单个
    async bookone() {
      const { ctx } = this;
      const { id } = ctx.request.body;
   
      if (!id) return ctx.body = { success: false, info: 'uid不能为空' };
      try {
        const one = await ctx.model.Foot.findByPk(id);
        console.log(one,'我是结构');
        ctx.body = { success: true, data: one, info: '查询成功' };
      } catch (e) {
        console.log(e,'我是错误');
        ctx.body = { success: false, info: '查询失败' };
      }
    }
 

     //查询全部
 
  async findAll(){
    const { ctx } = this;
    let { page,limit } = ctx.request.body;
    console.log(page,'我是page');
    
    let a =await ctx.model.Foot.count()
    console.log(a,'我是总数');
    page = page || 1;
    limit = limit || 6;
    const offset = (page - 1) * limit;

    try{
           // 如果不传参数默认查询全部
      let res = await ctx.model.Foot.findAll({
          offset, // 查询偏移量(起点)
          limit, // 查询的返回的记录
      })
    ctx.body = { success: true, data: res, all:a,  info: '查询成功' };
    }catch(e){
      console.log(e,'我是错误');
      ctx.body = { success: false, info: '查询失败' };
    }

  
}





  // 修改
  async update() {
    const { ctx } = this;
    // const uid = 1; // todo 以后的uid 通过token解密获取
    // uid,
    // name,
    // cover,
    // wirter,
    // type,
    // dec,
    // recommended,
    // general,
    // no,
    // readnum,
    // wrodnum,
    // humnum
    const { uid, ...updateData } = ctx.request.body; // 得到post请求的body参数

    console.log(ctx.request.body,'我是uid');
    // console.log(updateData,'我是updateData');
    // 数据过滤
    if (!uid) return ctx.body = { success: false, info: 'uid不能为空?' };


    try {
      await ctx.model.Foot.update(updateData, {
        where: {
          uid,
        },
      });

      ctx.body = { success: true, info: '修改成功' };

    } catch (e) {
      console.log(e,'我是错误');
      ctx.body = { success: false, info: '修改失败' };
    }

  }

  //根据经纬度查店铺
  async query(){
      const { ctx } = this;
      const { lat,lng } = ctx.request.body; 

      
      try {

        const res = await ctx.model.query(`SELECT *,
        (st_distance(point(lat, lng),point(${lat},${lng}))*111195) AS distance
         FROM foot 
        HAVING distance<1000
        ORDER BY distance`);

        console.log(res,'我是res')

        ctx.body = { success: true, data: res[0], info: '查询成功' };
      }catch(e){
        console.log(e,'我是错误信息');
        ctx.body = { success: false, info: '查询失败' };
      }

  }



  
}

module.exports = BuildController;
